﻿using System;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.TeamFoundation.VersionControl.Client;
using TfsBranchLib2.Model;
using TfsComLib.TfsBranchLib2;

namespace TfsBranchLib2.View
{
    /// <summary>
    ///     Interaction logic for BranchedHistoryControl.xaml
    ///     Main collecting control for Branched History Functionality
    /// </summary>
    public partial class BranchedHistoryControl : UserControl
    {
        private IBranchedHistory branchedHistoryContext;

        public BranchedHistoryControl()
        {
            InitializeComponent();
            SetBranchedHistoryContext(BranchedHistoryContext.DefaultBranchedHistoryContext);
        }

        private void SetBranchedHistoryContext(BranchedHistoryContext branchedHistoryContext)
        {
            this.branchedHistoryContext = branchedHistoryContext;
            inputTab.SetBindings(branchedHistoryContext);
            outputTab.SetBindings(branchedHistoryContext);
            errorMessage.DataContext = branchedHistoryContext;
            errorMessage.SetBinding(VisibilityProperty,
                new Binding("IsError") {Mode = BindingMode.OneWay, Converter = new BooleanToVisibilityConverter()});
            errorMessage.SetBinding(TextBox.TextProperty, new Binding("ErrorMessage") {Mode = BindingMode.OneWay});
            branchedHistoryContext.OnProcessCompleted += branchedHistoryContext_OnProcessCompleted;
            outputTab.OnCompare += outputTab_OnCompare;
            outputTab.OnShowChangesetDetails += outputTab_OnShowChangesetDetails;
        }

        private void outputTab_OnShowChangesetDetails(object source, BranchedHistoryOutputControl.ChangesetDetailsEventArgs args)
        {
            if (OnShowChangesetDetails != null)
            {
                OnShowChangesetDetails(this, args);
            }
        }

        private void outputTab_OnCompare(object sender, BranchedHistoryOutputControl.CompareEventArgs e)
        {
            if (OnCompare != null)
            {
                OnCompare(sender, e);
            }
        }

        public event EventHandler<BranchedHistoryOutputControl.ChangesetDetailsEventArgs> OnShowChangesetDetails;

        public event EventHandler<BranchedHistoryOutputControl.CompareEventArgs> OnCompare;

        private void branchedHistoryContext_OnProcessCompleted(object sender, EventArgs e)
        {
            Dispatcher.Invoke(() =>
            {
                if (!branchedHistoryContext.IsError)
                {
                    tabControl.SelectedItem = outputTabItem;
                }
            });
        }

        public void UpdateContextData(string path, VersionControlServer versionControlServer)
        {
            branchedHistoryContext.QueryType = BranchedHistoryQueryType.SingleSubPath;
            branchedHistoryContext.SelectedBranchesCache.SourceBranchesProvider.TfsConnection.SetVersionControlServer(
                versionControlServer);
            branchedHistoryContext.SelectedBranchesCache.SourceBranchesProvider.TfsConnection.TestConnect();
            branchedHistoryContext.SinglePath = path;
        }
    }
}